<template>
	<el-tabs v-model="activeName">
		<el-tab-pane label="项目基础信息" name="first">
			<el-form :model="Engineering_Project" label-width="120px" style="padding: 20px 0px" ref="Engineering"
				:rules="Engineering_rules">
				<el-row>
					<el-col :span="11">
						<el-form-item label="项目名称" prop="ProjectName">
							<el-input v-model="Engineering_Project.ProjectName" placeholder="项目名称"></el-input>
						</el-form-item>
					</el-col>
					<el-col :span="11"  :offset="1">
						<el-form-item label="项目名称缩写" prop="ProjectNameSX">
							<el-input v-model="Engineering_Project.ProjectNameSX" placeholder="项目名称缩写"></el-input>
						</el-form-item>
					</el-col>
					
				</el-row>
				<el-row>
					<el-col :span="11">
						<el-form-item label="工程地址">
							<el-input v-model="Engineering_Project.ProjectAddress" placeholder="工程地址"></el-input>
						</el-form-item>
					</el-col>
					<el-col :span="11" :offset="1">
						<el-form-item label="施工单位">
							<el-input v-model="Engineering_Project.ConstructionUnit" placeholder="施工单位"></el-input>
						</el-form-item>
					</el-col>
				</el-row>
				<el-row>
					<el-col :span="11">
						<el-form-item label="合同总价" prop="EstimatedContractAmount">
							<el-input v-model="Engineering_Project.EstimatedContractAmount"
								placeholder="合同总价"></el-input>
						</el-form-item>
					</el-col>
					<el-col :span="11" :offset="1">
						<el-form-item label="项目负责人" prop="HeadUserId">
							<el-select v-model="Engineering_Project.HeadUserId" filterable placeholder="请选择负责人"
								:disabled="disabled" style="width: 100%; ">
								<el-option v-for="item in HandUserIds" :value="item.Id"
									:label="item.UserName"></el-option>
							</el-select>
						</el-form-item>
					</el-col>
				</el-row>
				<el-row>
					<el-col :span="11">
						<el-form-item label="实际合同(金额)" prop="ContractamountInside">
							<el-input v-model="Engineering_Project.ContractamountInside" placeholder="实际合同"
								@blur="SettleMoneyHandler()"></el-input>
						</el-form-item>
					</el-col>
					
					<el-col :span="11" :offset="1">
						<el-form-item label="实际结算金额" prop="SettleMoney">
							<el-input v-model="Engineering_Project.SettleMoney" placeholder="实际合同额" @blur="guanlifei()"></el-input>
						</el-form-item>
					</el-col>
					
				</el-row>
				<el-row>
					<el-col :span="11">
						<el-form-item label="补充合同(金额)" prop="ContractamountExternal">
							<el-input v-model="Engineering_Project.ContractamountExternal" placeholder="补充合同"
								@blur="guanlifei()"></el-input>
						</el-form-item>
					</el-col>
					<el-col :span="11" :offset="1">
						<el-form-item label="管理比例" prop="Managementproportion">
							<el-input v-model="Engineering_Project.Managementproportion" placeholder="管理费"
								@blur="guanlifei()">
								<template slot="append">%</template>
							</el-input>
						</el-form-item>
					</el-col>
					
				</el-row>
				<el-row>
					<el-col :span="11">
						<el-form-item label="管理费" prop="Managementfee">
							<el-input v-model.sync="Engineering_Project.Managementfee" placeholder="管理费"
								disabled></el-input>
						</el-form-item>
					</el-col>
					<el-col :span="11" :offset="1">
						<el-form-item label="管理费分摊" prop="ManagementfeeFT">
							<el-input v-model.sync="Engineering_Project.ManagementfeeFT" placeholder="例如：2024-10000；2025-20000"
								></el-input>
						</el-form-item>
					</el-col>
				</el-row>
				<el-row>
					<el-col :span="11">
						<el-form-item label="开始日期" prop="StartDate">
							<el-date-picker style="width: 100%" @change="changedateval"
								v-model="Engineering_Project.StartDate" type="date" value-format="yyyy-MM-dd hh:mm:ss"
								placeholder="选择日期">
							</el-date-picker>
						</el-form-item>
					</el-col>
					<el-col :span="11" :offset="1">
						<el-form-item label="结束日期" prop="EndDate">
							<el-date-picker style="width: 100%" @change="changedateval"
								v-model="Engineering_Project.EndDate" type="date" value-format="yyyy-MM-dd hh:mm:ss"
								placeholder="选择日期">
							</el-date-picker>
						</el-form-item>
					</el-col>
				</el-row>
				<el-row>
					
					<el-col :span="11">
						<el-form-item label="备注">
							<el-input v-model="Engineering_Project.Remark" placeholder="备注"></el-input>
						</el-form-item>
					</el-col>
				</el-row>
				<el-row>
					<el-col :span="11">
						<el-form-item label="合同文件上传">
							<UploadFileF ref="AnotherData" :multiple="true" :another-data="AnotherData"
								:showfilelist="true" :upshow="false" content="上传合同文件" btnsize="medium"></UploadFileF>
						</el-form-item>
					</el-col>
				</el-row>
				<el-row v-if="!filetype">
					<el-col>
						<!-- 上传文件资源 -->
						<el-table :data="fileData" style="width: 100%" ref="multipleTable" border
							:row-style="{ height: '20px' }" :cell-style="{ padding: '5px 0 ' }">
							<el-table-column header-align="center" type="selection" width="50"
								align="center"></el-table-column>
							<el-table-column header-align="center" fixed prop="FileName" label="文件名称"
								align="center"></el-table-column>
							<el-table-column header-align="center" prop="Date" label="上传时间"
								align="center"></el-table-column>
							<el-table-column header-align="center" prop="Size" label="文件大小"
								align="center"></el-table-column>
							<el-table-column header-align="center" label="操作" align="center" width="210">
								<template slot-scope="scope">
									<el-button @click="DownFile(scope.row)" type="text" size="small">
										下载
									</el-button>
									<el-button @click="DeleFile(scope.row)" type="text" size="small" style="color:#F56C6C">
										删除
									</el-button>
								</template>
							</el-table-column>
						</el-table>
					</el-col>
				</el-row>
				<el-row>
					<el-col :span="22" :offset="1" align="center">
						<el-button type="success" @click="AddSubmit('Engineering')">保存</el-button>
					</el-col>
				</el-row>

			</el-form>
		</el-tab-pane>
	</el-tabs>
</template>

<script>
	import * as publicjs from "../../../public/Scriput/Public";
	import UploadFileF from "../UploadFileF";
	export default {
		name: "ProjectFiling",
		components: {
			UploadFileF,
		},
		props: {
			UpProject: {
				type: Object,
				required: true
			},
			filetype: {
				type: Boolean,
				required: true
			},

		},
		watch: {
			UpProject: {
				handler(val) {
					this.getHeadUserList(val)					
					this.Engineering_Project = {
						...val
					};
					this.init(val);
				},
				deep: true,
				immediate: true,
			},
			filetype: function(val) {
				console.log(111);
			}
		},

		data() {
			return {
				disabled: true,
				activeName: "first",
				fileData: [],
				fileList: [],
				AnotherData: {
					//项目模块
					FileType: 2,
					//项目Id号
					id: 0,
					//Type：1：项目提前批资料、2：收方工资单   3、应发工资单、4报销单 
					Type: 1,
					Single: false,
				},
				HandUserIds: [],
				activeTop: 'Engineering',
				//所有的项目单位属性
				AllProjectUnitAttribute: [],
				AllDepartment: [],
				UnitAttributeLis: [],
				//工程技术生产类模板
				Engineering_Project: {
					HeadUserId: "",
					ProjectNumber: '',
					ProjectName: '',
					PartA: '',
					EstimatedContractAmount: 0,
					CollectionAmount: '0',
					ProjectNameSX:'',
					Remark: '',
					StartDate: '',
					EndDate: '',
					Managementfee: 0,
					ManagementfeeFT:'',
					Id: 0,
					SettleMoney: 0,
					ContractamountInside: 0,
					ContractamountExternal: 0,
					Managementproportion: 0,
				},
				Engineering_rules: {
					StartDate: [{
						required: true,
						message: '此为必填项',
						trigger: 'blur'
					}, ],
					EndDate: [{
						required: true,
						message: '此为必填项',
						trigger: 'blur'
					}, ],
					EndDate: [{
						required: true,
						message: '此为必填项',
						trigger: 'blur'
					}, ],
					EndDate: [{
						required: true,
						message: '此为必填项',
						trigger: 'blur'
					}, ],
					EstimatedContractAmount: [{
							required: true,
							message: '此为必填项',
							trigger: 'blur'
						},

					],
					Managementfee: [{
							required: true,
							message: '此为必填项',
							trigger: 'blur'
						},

					],
					HeadUserId: [{
						required: true,
						message: '此为必填项',
						trigger: 'blur'
					}, ],
					ProjectName: [{
						required: true,
						message: '此为必填项',
						trigger: 'blur'
					}, ],
					ProjectNameSX: [{
						required: true,
						message: '此为必填项',
						trigger: 'blur'
					}, ],
				},
			}
		},
		created() {
			this.AllProjectUnitAttribute = publicjs.GetAllProjectUnitAttribute(this.$http)
			this.AllDepartment = publicjs.GetAllDepartment(this.$http)
			publicjs.http(this.$http, this.$message)
		},
		methods: {
			//文件下载
			DownFile(row) {
				console.log(row, '??????????????????????')
				// let linkHref = this.$BasePath + row.Url;
				// console.log(linkHref);
				// let isWindow = false;
				// try {
				// 	isWindow = command.getBtnState();
				// } catch (error) {}
				// if (isWindow) {
				// 	command.downLoadResult(linkHref);
				// 	return;
				// }
				// var link = document.createElement("a");
				// link.href = linkHref;
				// document.body.appendChild(link);
				// link.click();
				// document.body.removeChild(link);
				let Data = {
					id: this.UpProject.Id,
					FileType: 2,
					Type: 1,
					FileName: row.FileName,
				}
				//文件下载
				publicjs.DownLoadFile({
					...Data
				})
			},
			//删除文件
			DeleFile(row) {
				console.log(row);
				const {
					Url
				} = row || ''
				if (Url != '') {
					this.$http.post('StaticFile/DeleteFileByUrl', {
						filePath: row.Url
					}).then(res => {
						const {
							StatusCode
						} = res.data || 0
						if (StatusCode == 200) {
							this.$message.success('删除成功!')
							this.init(this.UpProject)
						} else this.$message.warning('删除失败,请重试!')
					})
				}
			},
			init(val) {
				let _this = this;
				if (!_this.filetype) {
					_this.$http.post('Project1/GetFileNames', {
						id: _this.UpProject.Id
					}).then(res => {
						_this.fileData = res.data.Data;
						console.log(res.data.Data);
					}).catch(e => {
						_this.$message.error('服务器出现问题');
					});

				}

			},
			changedateval() {
				const _this = this;
				console.log(_this.Engineering_Project.StartDate);
				console.log(_this.Engineering_Project.EndDate);
				console.log(1);
				if (_this.Engineering_Project.StartDate != '' && _this.Engineering_Project.EndDate != '') {
					_this.disabled = false;
					_this.$http.post('Project1/GetNotDateUser', {
						StartDate: this.Engineering_Project.StartDate,
						EndDate: this.Engineering_Project.EndDate,
						// Add:_this.filetype,
					}).then(res => {
						_this.HandUserIds = res.data.Data
						console.log(res.data.Data);
					}).catch(e => {
						_this.$message.error('服务器出现问题');
					});
				}
			},
			AddSubmit(type) {
				let _this = this
				let data
				if (type == "Engineering") {
					data = this.Engineering_Project
				} else {
					data = this.Production_Project
				}
				if (_this.filetype) {
					let Unitstring = ''
					this.UnitAttributeLis.forEach(item => {
						Unitstring += item + ','
					})
					data.UnitAttribute = Unitstring.substring(0, Unitstring.length - 1)
					this.$refs[type].validate((res) => {
						if (res) {
							_this.$http.post('Project1/AddProjectRecord', {
								Project: data
							}).then(res => {
								if (res.data) {
									if (this.$refs['AnotherData'].fileList.length > 0) {
										_this.AnotherData.id = res.data.Data.Id;
										this.$refs['AnotherData'].submitUpload()
									}
									_this.$message.success('添加成功')
									_this.$parent.$parent.Project_filing_dialog = false
									_this.UnitAttributeLis = []
									_this.$parent.$parent.formdata()
									this.Engineering_Project = {}
									_this.disabled = true;
								} else {
									_this.$message.warning('添加失败!')
								}
							}).catch(() => {
								_this.$message.error('服务器异常!')
							})
						}
					})
				} else {
					//项目文件修改
					this.$refs[type].validate((res) => {
						if (res) {
							_this.$http.post('Project1/UpdateProjectRecord', {
								Project: data
							}).then(res => {
								if (res.data) {
									if (this.$refs['AnotherData'].fileList.length > 0) {
										_this.AnotherData.id = data.Id;
										this.$refs['AnotherData'].submitUpload()
									}
									_this.$message.success('修改成功')
									_this.$parent.$parent.Project_filing_dialog = false
									_this.UnitAttributeLis = []
									_this.$parent.$parent.formdata()
									this.Engineering_Project = {}
									_this.disabled = true;
								} else {
									_this.$message.warning('修改失败!')
								}
							}).catch(() => {
								_this.$message.error('服务器异常!')
							})
						}
					})

				}
			},
			//管理费
			guanlifei() {
				let {
					Managementproportion
				} = this.Engineering_Project || 0
				if (!publicjs.CheckAmount(Managementproportion)) {
					this.$message.warning('请输入有效的金额比例!')
					this.Engineering_Project.Managementproportion = ''
				} else {
					if (Managementproportion > 100) this.$message.warning('管理费金额比例异常!')
					else this.Engineering_Project.Managementfee = ((Managementproportion / 100) * (Number(this.Engineering_Project.SettleMoney)+Number(this.Engineering_Project
						.ContractamountExternal))).toFixed(2)
				}
			},
			//合同费
			// SettleMoneyHandler() {
			// 	let {
			// 		ContractamountInside,
			// 		ContractamountExternal
			// 	} = this.Engineering_Project || 0
			// 	this.Engineering_Project.SettleMoney = (Number(ContractamountInside) + Number(ContractamountExternal))
			// 		.toFixed(2)
			// },
			//设置人员列表Engineering_Project.HeadUserList(){
			getHeadUserList(obj){
				const { HeadUserId } = obj || 0
				if(HeadUserId!=undefined){
					this.HandUserIds = this.$store.state.AllUsers.filter(x=>x.Id == HeadUserId)
					console.log(this.HandUserIds,'>>>>>>>>>>>>>>>>>>>>>>>>>>>')
				}
			}
		}
	}
</script>

<style scoped></style>